import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAddonBaseService extends BaseService<any> {
  private readonly logger = new Logger(CoreAddonBaseService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getAddonConfig
   * 对应 PHP: CoreAddonBaseService_core::getAddonConfig()
   * 逻辑类型: undefined - undefined
   */
  async getAddonConfig(addon: string) {
    // 基于PHP真实逻辑: getAddonConfig
    // PHP原文: $path = $this->addon_path . $addon . DIRECTORY_SEPARATOR . 'info.json';         $resource_path = $this->addon_path . $addon . DIRECTORY_SEPARATOR . 'r...
const path = this.addon_path . addon . DIRECTORY_SEPARATOR  + 'info.json';
        const resource_path = this.addon_path . addon . DIRECTORY_SEPARATOR  + 'resource' . DIRECTORY_SEPARATOR;
        if (is_file(path)) {
            const json_string = file_get_contents(path];
            // 用参数true把JSON字符串强制转成PHP数组
            const info = json_decode(json_string, true);
            info.icon = resource_path  + 'icon.png';
            info.cover = resource_path  + 'cover.png';
  }

  /**
   * getAddonConfigPath
   * 对应 PHP: CoreAddonBaseService_core::getAddonConfigPath()
   * 逻辑类型: undefined - undefined
   */
  async getAddonConfigPath(addon: string) {
    // 基于PHP真实逻辑: getAddonConfigPath
    // PHP原文: return $this->addon_path . $addon . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR;     }      /**      * 获取插件定义的package目录      * @param string ...
return this.addon_path . addon . DIRECTORY_SEPARATOR  + 'config' . DIRECTORY_SEPARATOR;
    }

    /**
     * 获取插件定义的package目录
     * @param string addon
     * @return string
     */
    async geAddonPackagePath(string addon)
    {
        return this.addon_path . addon . DIRECTORY_SEPARATOR  + 'package' . DIRECTORY_SEPARATOR;
    }

    /**
     * 读取json文件转化成数组返回
     * @param json_file_path //json文件目录
     */
    async jsonFileToArray(string json_file_path)
    {
        if (file_exists(json_file_path)) {
            const content_json = @file_get_contents(json_file_path];
            return json_decode(content_json, true);
  }

  /**
   * geAddonPackagePath
   * 对应 PHP: CoreAddonBaseService_core::geAddonPackagePath()
   * 逻辑类型: undefined - undefined
   */
  async geAddonPackagePath(addon: string) {
    // 基于PHP真实逻辑: geAddonPackagePath
    // PHP原文: return $this->addon_path . $addon . DIRECTORY_SEPARATOR . 'package' . DIRECTORY_SEPARATOR;     }      /**      * 读取json文件转化成数组返回      * @param $json_f...
return this.addon_path . addon . DIRECTORY_SEPARATOR  + 'package' . DIRECTORY_SEPARATOR;
    }

    /**
     * 读取json文件转化成数组返回
     * @param json_file_path //json文件目录
     */
    async jsonFileToArray(string json_file_path)
    {
        if (file_exists(json_file_path)) {
            const content_json = @file_get_contents(json_file_path];
            return json_decode(content_json, true);
  }

  /**
   * jsonFileToArray
   * 对应 PHP: CoreAddonBaseService_core::jsonFileToArray()
   * 逻辑类型: undefined - undefined
   */
  async jsonFileToArray(json_file_path: string) {
    // 基于PHP真实逻辑: jsonFileToArray
    // PHP原文: if (file_exists($json_file_path)) {             $content_json = @file_get_contents($json_file_path);             return json_decode($content_json, tru...
if (file_exists(json_file_path)) {
            const content_json = @file_get_contents(json_file_path];
            return json_decode(content_json, true);
  }

  /**
   * writeArrayToJsonFile
   * 对应 PHP: CoreAddonBaseService_core::writeArrayToJsonFile()
   * 逻辑类型: undefined - undefined
   */
  async writeArrayToJsonFile(content: any[], file_path: any[]) {
    // 基于PHP真实逻辑: writeArrayToJsonFile
    // PHP原文: $content_json = json_encode($content, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);         $content_json = preg_replace('/\[\...
const content_json = json_encode(content, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        const content_json = preg_replace('/\[\)/', '{
    // 待实现
  }', content_json];
        const result = @file_put_contents(file_path, content_json);
        if (!result) {
            throw new BusinessException(file_path  + '文件不存在或者权限不足');
  }
}
